using System.Diagnostics;
namespace 第四次作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetListView();
            load_C();
        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {
        }


        private void SetListView()
        {
            listView1.Columns.Add("名称", 8 * listView1.Width / 13);
            listView1.Columns.Add("大小", 3 * listView1.Width / 13);
            listView1.Columns.Add("类型", 2 * listView1.Width / 13);
        }

        private void load_C()
        {
            DriveInfo[] drivers = DriveInfo.GetDrives();
            foreach (DriveInfo driver in drivers)
            {
                if (driver.Name == "C:\\")
                {
                    TreeNode topnode = new TreeNode(driver.Name);
                    treeView1.Nodes.Add(topnode);
                    topnode.ImageIndex = 0;
                    topnode.Nodes.Add("");
                    break;
                }
            }
        }
        private void add_childNode(TreeNode parentNode)
        {
            DirectoryInfo directory = new DirectoryInfo(parentNode.FullPath);
            try
            {
                parentNode.Nodes.RemoveAt(0);
                foreach (FileSystemInfo fsinfo in directory.GetFileSystemInfos())
                {
                    if (fsinfo is FileInfo)
                    {
                        TreeNode childNode = new TreeNode(fsinfo.Name);
                        childNode.ImageIndex = 2;
                        parentNode.Nodes.Add(childNode);
                    }
                    else if (fsinfo is DirectoryInfo)
                    {
                        TreeNode childNode = new TreeNode(fsinfo.Name);
                        childNode.Name = fsinfo.FullName;
                        childNode.ImageIndex = 1;
                        parentNode.Nodes.Add(childNode);
                        childNode.Nodes.Add("");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            add_childNode(e.Node);
        }

        private string GetPathFromNode(TreeNode node)
        {
            if (node == null)
            {
                return node.Name;
            }
            else
            {
                return node.FullPath;
            }
        }


        private void directoryTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                long length;
                string path;
                TreeNode node = e.Node;
                listView1.Items.Clear();
                path = GetPathFromNode(node);
                listView1.BeginUpdate();
                //实例目录与子目录
                DirectoryInfo dir = new DirectoryInfo(path);
                //获取当前目录文件列表
                FileInfo[] fileInfo = dir.GetFiles();
                DirectoryInfo[] directoryInfos = dir.GetDirectories();
                for (int i = 0; i < directoryInfos.Length; i++)
                {
                    ListViewItem listItem = new ListViewItem();
                    listItem.Text = directoryInfos[i].Name;    //显示文件名
                    listItem.ForeColor = Color.Blue;                            //设置行颜色
               
                    //获取文件扩展名时可用Substring除去点 否则显示".txt文件"
                    listItem.SubItems.Add(directoryInfos[i].Extension + "文件");
                    //加载数据至filesList
                    listView1.Items.Add(listItem);
                }
                //结束数据处理,UI界面一次性绘制 否则可能出现闪动情况
                listView1.EndUpdate();
                //循环输出获取文件信息
                for (int i = 0; i < fileInfo.Length; i++)
                {
                    ListViewItem listItem = new ListViewItem();
                    listItem.Text =  fileInfo[i].Name;    //显示文件名
                    listItem.ForeColor = Color.Blue;                            //设置行颜色

                    //length/1024转换为KB字节数整数值 Ceiling返回最小整数值 Divide除法
                    length = fileInfo[i].Length;                                //获取当前文件大小
                    listItem.SubItems.Add(Math.Ceiling(decimal.Divide(length, 1024)) + " KB");

                    //获取文件扩展名时可用Substring除去点 否则显示".txt文件"
                    listItem.SubItems.Add(fileInfo[i].Extension + "文件");
                    //加载数据至filesList
                    listView1.Items.Add(listItem);
                }
                //结束数据处理,UI界面一次性绘制 否则可能出现闪动情况
                listView1.EndUpdate();
            }
            catch (Exception msg)  //异常处理
            {
                MessageBox.Show(msg.Message);
            }

        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems[0].SubItems[0].Text.EndsWith(".exe"))
            {
                Process.Start(GetPathFromNode(treeView1.SelectedNode)+"\\"+ listView1.SelectedItems[0].Text);
            }
            if (listView1.SelectedItems[0].SubItems[0].Text.EndsWith(".txt"))
            {
                Process.Start("notepad.exe",GetPathFromNode(treeView1.SelectedNode)+ "\\" + listView1.SelectedItems[0].Text);
            }
        }
    }
}